/**
 * Created by shidaiapp on 2017/9/29.
 */

function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalModify.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalModify.isLoading = true;
  });

  tiyData.tStatus.modalDelete.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDelete.isLoading = true;
  });

}


/**
 * 为Tree准备结构化的数据
 * @param self
 * @param tiyData
 */
function readyAuthorites(self,tiyData,selectedAuths) {
  tiyData.tTreeDatas.authTreeData = [];
  let authorities = tiyData.tTreeDatas.authRoles;
  let moduleList = self._.filter(authorities,function(o) { return o.type===1; });

  self._.forEach(moduleList,function (moudle) {

    var moduleChild = [];

    self._.forEach(findChildenAuths(self,tiyData,moudle.type,moudle.id),function (page) {

      var pageChild = [];

      self._.forEach(findChildenAuths(self,tiyData,page.type,page.id),function(rule){

        pageChild.push(prepareTreeItem(false,rule.type,rule.name,rule))


      });

      moduleChild.push(prepareTreeItem(false,page.type,page.name,page,pageChild));
    });

    self.authTreeData.push( prepareTreeItem(false,moudle.type, moudle.name,moudle,moduleChild));

  });
}

function prepareTreeItem(expand,level,title,extra,children) {
  return {
    expand:expand,
    level:level,
    title:title,
    extra:extra,
    children:children
  };
}


/**
 *  查询子级别的权限
 * @param self
 * @param tiyData
 * @param type
 * @param id
 * @return {*}
 */
function findChildenAuths(self,tiyData,type,id) {

  let childAuths = self._.filter(tiyData.tTreeDatas.authRoles,
    function(o) {
    let childType = type+1;
    return o.type===childType && o.parentId===id;
  });

  return childAuths ;

}




function resetForm(self,tiyData) {
  self.$refs['modifyRoleItem'].resetFields();
}

/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.roleNameSearch = '';

};


/**
 * 添加新角色
 * @param self
 * @param tiyData
 */
export const add = (self, tiyData) => {
  // makeRoleTreeData(self,tiyData,false);
  resetForm(self,tiyData);
  self.tempSelectedRules = [];
  tiyData.tParams.role.id = "";
  tiyData.tParams.role.name = "";
  tiyData.tParams.role.yunFolders = "";
  tiyData.tParams.role.note = "";
  tiyData.tParams.role.roleFunctions = [];
  tiyData.tStatus.modalModify.isShow= true;
  tiyData.tStatus.modalModify.isModify= false;
  tiyData.tStatus.modalModify.title= '新建角色';

};

export const modify = (self, tiyData, index) => {
  resetForm(self,tiyData);
  self.$Spin.show();
  self.$apiRole.getRoleById({
    id: self.roleList[index].id
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      var role = response.result;
      tiyData.tParams.role.id = role.id;
      tiyData.tParams.role.name = role.name;
      tiyData.tParams.role.yunFolders = role.yunFolders;
      tiyData.tParams.role.note = role.note;
      tiyData.tParams.role.roleFunctions = role.roleFunctions;

      var authList = role.authRulers;
      self.tempSelectedRules = [];
      self._.forEach(authList,function (item) {
        self.tempSelectedRules.push(prepareTreeItem(false,item.type,item.name,item));
      });

      // makeRoleTreeData(self,tiyData,true);
      tiyData.tStatus.modalModify.isShow = true;
      tiyData.tStatus.modalModify.isModify= true;
      tiyData.tStatus.modalModify.title= '编辑角色';
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
}

/**
 * 删除
 * @param self
 * @param tiyData
 */
export const deleted = (self, tiyData,index) => {
  tiyData.tStatus.modalDelete.isShow = true;
  tiyData.tStatus.modalDelete.role = tiyData.tData.roleList[index];
};

/**
 * 添加/修改用户
 * @param self
 * @param tiyData
 * @param name
 */
export const webAddOrModify = function (self, tiyData, name) {
  tiyData.tStatus.modalModify.isLoading = true;
  self.$refs[name].validate((valid) => {
    if (valid) {
      let rules = [];
      self._.forEach(self.tempSelectedRules,function (item) {
        rules.push(item.extra.id);
      });

      var parms = {
        id: tiyData.tParams.role.id,
        name: tiyData.tParams.role.name,
        yunFolders: "",
        rules:rules,
        note:tiyData.tParams.role.note
      };
      self.$apiRole.addRole(parms)
        .then(function (response) {
          changeModelLoading(self, tiyData);
          tiyData.tStatus.modalModify.isShow= false;
          if (response.success) {
            webList(self, tiyData);
            self.$Message.success('提交成功!');
          } else {
            self.$Message.error(response.errorMessage);
          }
        });

    } else {
      changeModelLoading(self, tiyData);
    }
  });
};


export const webDelete = function (self, tiyData) {
  var parms = {
    id: tiyData.tStatus.modalDelete.role.id,
    name: tiyData.tStatus.modalDelete.role.name
  };
  self.$apiRole.deleteRole(parms)
    .then(function (response) {
      tiyData.tStatus.modalDelete.isShow = false;
      if (response.success) {
        changeModelLoading(self, tiyData);
        webList(self, tiyData);
        self.$Message.success('删除成功!');
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

export const webList = (self, tiyData) => {
  self.loadingData = true;
  self.$Spin.show();
  self.$apiRole.getRoleListPage({
    "pageSize": 20,
    "pageNum": tiyData.tPage.currentPage,
    "name": tiyData.tParams.roleNameSearch,
  })
    .then(function (response) {
      self.$Spin.hide();
      self.loadingData = false;
      if (response.success) {
        tiyData.tData.roleList.splice(0);
        let rolePage = response.result;
        tiyData.tPage.total = rolePage.total;
        tiyData.tPage.stepNumber  = rolePage.pageSize;
        rolePage.list.forEach(
          function (item) {
            tiyData.tData.roleList.push(item);
          }
        );
        if(0 === tiyData.tData.roleList.length){
          // self.$Message.error("查询没有结果");
        }
      } else {
        self.$Message.error(response.errorMessage);
      }
    });

};
export const webModuleFunctionList = (self,tiyData) =>{
  // self.$Spin.show();
  self.$apiRole.getModuleFunctionList()
    .then(function (response) {
      if (response.success) {
        let authRoles = [];
        for (var j = 0; j < response.result.authRoles.length; j++) {
               authRoles.push(response.result.authRoles[j]);
            }
        tiyData.tTreeDatas.authRoles = authRoles;
        readyAuthorites(self,tiyData);
      }else{
        self.$Message.error(response.errorMessage);
      }
      // self.$Spin.hide();
    });









};
